#include "CourseCard.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGraphicsDropShadowEffect>
#include <QPainter>

CourseCard::CourseCard(const QString &title,
                       const QString &time,
                       const QString &students,
                       const QString &cover,
                       int status,
                       QWidget *parent)
    : QWidget(parent)
{
    setFixedHeight(120);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    /* 阴影 */
    auto *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setBlurRadius(12);
    shadow->setColor(QColor(0, 0, 0, 60));
    shadow->setOffset(0, 4);
    setGraphicsEffect(shadow);

    /* 封面图 */
    QLabel *img = new QLabel;
    img->setFixedSize(90, 90);
    img->setScaledContents(true);
    if (cover.isEmpty())
        img->setPixmap(QPixmap(":/images/default.png"));
    else
        img->setPixmap(QPixmap(cover));

    /* 文字标签 */
    QLabel *labTitle = new QLabel(title);
    labTitle->setStyleSheet("font-weight:bold;font-size:15px;");
    QLabel *labTime  = new QLabel(time);
    labTime->setStyleSheet("color:#666;");
    QLabel *labStu   = new QLabel(students);
    labStu->setStyleSheet("color:#999;");

    /* 状态标签 */
    QLabel *labStatus = new QLabel;
    QString txt = status == 0 ? "待上课" : status == 1 ? "上课中" : "已结束";
    QString bg  = status == 0 ? "#409eff" : status == 1 ? "#67c23a" : "#909399";
    labStatus->setText(txt);
    labStatus->setStyleSheet(QString("color:#fff;background:%1;padding:2 6;border-radius:4px;").arg(bg));

    /* 右侧文字布局 */
    QVBoxLayout *right = new QVBoxLayout;
    right->addWidget(labTitle);
    right->addWidget(labTime);
    right->addWidget(labStu);
    right->addWidget(labStatus, 0, Qt::AlignLeft);

    /* 主布局 */
    QHBoxLayout *main = new QHBoxLayout(this);
    main->setContentsMargins(10, 10, 10, 10);
    main->addWidget(img);
    main->addLayout(right, 1);
}

/* 圆角背景 */
void CourseCard::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
    QPainterPath path;
    path.addRoundedRect(rect(), 8, 8);
    p.fillPath(path, Qt::white);
    p.setPen(QColor("#dcdfe6"));
    p.drawPath(path);
}
